package com.dragon7531.bdemo.service;

import com.dragon7531.bdemo.domain.MeAccount;
import com.dragon7531.bdemo.domain.YouAccount;
import com.dragon7531.bdemo.mapper.MeAccountMapper;
import com.dragon7531.bdemo.mapper.YouAccountMapper;
import com.dragon7531.bdemo.utils.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BaccountService {
    @Autowired
    private MeAccountMapper meAccountMapper;
    @Autowired
    private YouAccountMapper youAccountMapper;

    @Autowired
    private BaccountService baccountService;
    //同一个类中有两个方法分别为 A 和 B，方法 A 没有添加事务注解，而方法 B 添加了 @Transactional 事务注解，
    // 此时方法 A 直接调用方法 B，则方法 B 的事务会失效。
    public void readyBi(){
        //例如做了很多业务查询
        //touShouBi();
        //第一种方式
        //SpringContextHolder.getBean(BaccountService.class).touShouBi();
        //第二种方式
        baccountService.touShouBi();
    }
    @Transactional(rollbackFor = Exception.class)
    public void touShouBi(){
        //支持了2个币
        YouAccount youAccount=new YouAccount(1,2);
        //币数减少
        youAccountMapper.updateTouBnum(youAccount);
        //报错
        //int i=2/0;
        //收到2个币
        MeAccount meAccount=new MeAccount(1,2);
        meAccountMapper.updateShouBnum(meAccount);
    }
}
